package com.project.api.ym.service;

import com.project.api.common.Unit;
import com.project.api.ym.dao.OrderDao;
import com.project.api.ym.model.Order;
import com.project.api.ym.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.rmi.CORBA.Util;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Service
public class OrderService {
    @Autowired
    private OrderDao orderDao;

    public List GetOrder(Map condition, int offset, int limit, String orderby)throws Exception{
        Iterator<Map.Entry<String, Object>> entries = condition.entrySet().iterator();
        StringBuilder q = new StringBuilder();
        while(entries.hasNext()){
            Map.Entry<String, Object> entry = entries.next();
            String key = entry.getKey();
            Object value = entry.getValue();
            q.append(" and ");
            if(key.equalsIgnoreCase("stime")||key.equalsIgnoreCase("gtime")){
                q.append("`").append(key).append("`>=").append("'").append(value).append("'");
            }else if(key.equalsIgnoreCase("etime")||key.equalsIgnoreCase("otime")){
                q.append("`").append(key).append("`<=").append("'").append(value).append("'");
            }else if(key.equalsIgnoreCase("nick_name")||key.equalsIgnoreCase("user_name")||key.equalsIgnoreCase("phone")||key.equalsIgnoreCase("company")||key.equalsIgnoreCase("order_num")||key.equalsIgnoreCase("user")){
                q.append("`").append(key).append("`").append(" like '%").append(value).append("%'");
            }else if(key.equalsIgnoreCase("ctime")){
                long v=Long.valueOf(String.valueOf(value));
                String date= Unit.StampToDate(v);
                long d=Unit.DateToStamp(date);
                long ed=d+24*60*60*1000;
                q.append("`").append(key).append("`>=").append("'").append(d).append("'");
                q.append(" and ").append("`").append(key).append("`<").append("'").append(ed).append("'");
            }
            else{
                q.append("`").append(key).append("`=").append("'").append(value).append("'");
            }
        }
        String qs = q.toString();
        String where = "";
        if(!qs.equalsIgnoreCase("")){
//            where= qs.substring(0,qs.length()-1);
            where=qs;
        }
        if (orderby!=null) where = where + " order by "+orderby;
        where = where + " limit "+ offset + ","+limit;
        return orderDao.GetOrder(where);
    }

    public int GetOrderCount(Map condition)throws Exception{
        Iterator<Map.Entry<String, Object>> entries = condition.entrySet().iterator();
        StringBuilder q = new StringBuilder();
        while(entries.hasNext()){
            Map.Entry<String, Object> entry = entries.next();
            String key = entry.getKey();
            Object value = entry.getValue();
            q.append(" and ");
            if(key.equalsIgnoreCase("stime")||key.equalsIgnoreCase("gtime")){
                q.append("`").append(key).append("`>=").append("'").append(value).append("'");
            }else if(key.equalsIgnoreCase("etime")||key.equalsIgnoreCase("otime")){
                q.append("`").append(key).append("`<=").append("'").append(value).append("'");
            }else if(key.equalsIgnoreCase("nick_name")||key.equalsIgnoreCase("user_name")||key.equalsIgnoreCase("phone")||key.equalsIgnoreCase("company")||key.equalsIgnoreCase("order_num")||key.equalsIgnoreCase("user")){
                q.append("`").append(key).append("`").append(" like '%").append(value).append("%'");
            }else if(key.equalsIgnoreCase("ctime")){
                long v=Long.valueOf(String.valueOf(value));
                String date= Unit.StampToDate(v);
                long d=Unit.DateToStamp(date);
                long ed=d+24*60*60*1000;
                q.append("`").append(key).append("`>=").append("'").append(d).append("'");
                q.append(" and ").append("`").append(key).append("`<").append("'").append(ed).append("'");
            }
            else{
                q.append("`").append(key).append("`=").append("'").append(value).append("'");
            }
        }
        String qs = q.toString();
        String where = "";
        if(!qs.equalsIgnoreCase("")){
            where = qs;
//            where=qs.substring(0,qs.length()-1);
        }
        return orderDao.GetOrderCount(where);
    }

    public Order GetOrderById(long id){
        return orderDao.GetOrderById(id);
    }
    public int RefundOrder(String order_num,String refund_num,String refund_price,long rtime){
        return orderDao.RefundOrder(order_num,refund_num,refund_price,rtime);
    }
}
